package com.health.work.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.health.work.R;
import com.health.work.activity.MedicalDistrictActivity.ViewHolder;
import com.health.work.model.OrderBean;
import com.health.work.model.SellerBean;
import com.health.work.utils.AsyncImageLoader;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

/**
 * 商家用户界面
 * 
 * @author star
 * 
 */
public class SellerUserActivity extends Activity {

	private Button back;
	private Button introduce;
	private TextView name;
	private ImageView headerImageView;
	private ListView yudinglistView;
	private TextView integral;
	private TextView mainSail;
	private TextView noData;
	private String sellerName;
	private String headerUrl;
	private String intro;
	private String mainSailString;
	private int jifen;

	private final static int SUCCESSL = -1;
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private final static int NODATA = 3;
	private final static int NORDER = 4;
	public static ArrayList<OrderBean> orderList;
	private Thread thread;
	private String mobilePhoneNum;
	private AsyncImageLoader asyncImageLoader;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESSL:
				orderAdapter adapter=new orderAdapter(SellerUserActivity.this);
				yudinglistView.setAdapter(adapter);
				break;
			case SUCCESS:
				setData();
				break;
			case FAILE:
				TipsUtil.showAlert(SellerUserActivity.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(SellerUserActivity.this, null, "未知错误！");
				break;
			case NODATA:
				noData();
				break;
			case NORDER:
				noOrder();
				break;
			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.seller_layout);
		mobilePhoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		initPages();
		getData();
		getList();
	}

	private void initPages() {
		integral = (TextView) findViewById(R.id.jifen);
		back = (Button) findViewById(R.id.btnBack);
		mainSail = (TextView) findViewById(R.id.geyan);
		introduce = (Button) findViewById(R.id.introduce);
		name = (TextView) findViewById(R.id.name);
		headerImageView = (ImageView) findViewById(R.id.head);
		yudinglistView = (ListView) findViewById(R.id.doctorlist);
		noData = (TextView) findViewById(R.id.nodata);
		back.setOnClickListener(onClickListener);
		introduce.setOnClickListener(onClickListener);
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnBack:
				TipsUtil.showExit(SellerUserActivity.this);
				break;
			case R.id.introduce:
				Intent intent;
				intent = new Intent(SellerUserActivity.this,
						SellerIntroduceActivity.class);
				intent.putExtra("introduce", intro);
				startActivity(intent);
				break;
			default:
				break;
			}
		}

	};

	/**
	 * 请求网络数据
	 */
	private void getData() {

		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().SelectBusinessByMobilePhone(
						mobilePhoneNum, new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("SelectBusinessByMobilePhoneStr",
										object.toString() + "");
								if (object.toString() != null) {
									sellerName = (String) object
											.getString("Name");
									headerUrl = (String) object
											.getString("Photo");
									jifen = object.getInt("Integral");
									mainSailString = (String) object
											.getString("MainCommodities");
									handler.sendEmptyMessage(SUCCESS);
								}
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub

							}
						});
			}
		});
		thread.start();

	}

	/**
	 * 请求网络数据
	 */
	private void getList() {
		thread = new Thread(new Runnable() {

			@Override
			public void run() {

				new RequestParamtersUtil().GetPreProduct(mobilePhoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("GetPreProduct0", result.toString() + "");
								if ("0".equals(result)) {
									handler.sendEmptyMessage(NODATA);
								} else if ("1".equals(result)) {
									handler.sendEmptyMessage(NORDER);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
								if (array.toString() != null
										&& array.toString().length() > 0) {

									Log.e("GetPreProduct", array.toString()
											+ "00");
									orderList = new ArrayList<OrderBean>();
									for (int i = 0; i < array.length(); i++) {
										OrderBean bean = new OrderBean();
										bean.MemberMobilePhone = array
												.getJSONObject(i).optString(
														"MemberMobilePhone");
										bean.ProductName = array.getJSONObject(
												i).optString("ProductName");
										orderList.add(bean);
									}
									handler.sendEmptyMessage(SUCCESSL);
								}
							}
						});
			}
		});
		thread.start();
	}

	// /**
	// * 请求网络数据
	// */
	// private void getLists() {
	// thread = new Thread(new Runnable() {
	//
	// @Override
	// public void run() {
	//
	// new RequestParamtersUtil().SelectProduct("", "", "", "", "",
	// "", mobilePhoneNum, new DefaultJSONData() {
	//
	// @Override
	// public void parse(String result)
	// throws JSONException {
	// // TODO Auto-generated method stub
	// if ("0".equals(result)) {
	// handler.sendEmptyMessage(NODATA);
	// }
	// }
	//
	// @Override
	// public void parse(JSONObject object)
	// throws JSONException {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void parse(JSONArray array)
	// throws JSONException {
	// // TODO Auto-generated method stub
	// if(array.toString()!=null&&array.toString().length()>0){
	// handler.sendEmptyMessage(SUCCESSL);
	// }
	// }
	// });
	// }
	// });
	// thread.start();
	// }

	private void setData() {

		asyncImageLoader = new AsyncImageLoader();
		if (sellerName != null) {
			name.setText(sellerName);
		}
		if (headerUrl != null && !headerUrl.equals("")) {
			Drawable drawable = asyncImageLoader.loadImageFromUrl(headerUrl);
			if (drawable != null) {
				headerImageView.setImageDrawable(drawable);
			} else {
				headerImageView.setImageResource(R.drawable.head_scu);
			}
		}
		if (!"".equals(jifen)) {
			integral.setText("积分:" + jifen);
		}
		if (mainSailString != null) {
			mainSail.setText("主营:" + mainSailString);
		}

	}

	private void noData() {
		yudinglistView.setVisibility(View.GONE);
		noData.setVisibility(View.VISIBLE);
		noData.setText("尚未发布商品");
	}

	private void noOrder() {
		yudinglistView.setVisibility(View.GONE);
		noData.setVisibility(View.VISIBLE);
		noData.setText("暂无预定");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		TipsUtil.showExit(SellerUserActivity.this);
		return super.onKeyDown(keyCode, event);

	}

	private class orderAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private AsyncImageLoader asyncImageLoader;
		Activity context;

		public orderAdapter(Context context) {
			this.context = (Activity) context;
			asyncImageLoader = new AsyncImageLoader();
			inflater = this.context.getLayoutInflater();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return orderList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return orderList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHodler;
			if (convertView == null) {
				viewHodler = new ViewHolder();
				convertView = inflater.inflate(R.layout.order_item, parent,
						false);
				viewHodler.phoneNum = (TextView) convertView
						.findViewById(R.id.phoneNum);
				viewHodler.name = (TextView) convertView
						.findViewById(R.id.name);
			} else {
				viewHodler = (ViewHolder) convertView.getTag();
			}
			OrderBean bean = orderList.get(position);
			viewHodler.phoneNum.setText("会员手机：" + bean.MemberMobilePhone);
			viewHodler.name.setText("商品名称：" + bean.ProductName);
			convertView.setTag(viewHodler);
			return convertView;
		}

	}

	class ViewHolder {

		public TextView phoneNum;
		public TextView name;
	}
}
